package yue.shen.android.demo.view.banner

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager

class AutoLoopBanner : FrameLayout, ViewPager.OnPageChangeListener {

    companion object {
        private const val TAG = "AutoLoopBanner"
    }

    private var size = -1

    private var currentItem = -1

    private var viewPagerAdapter: AutoLoopBannerAdapter? = null

    private val viewPager: AutoLoopViewPager by lazy { AutoLoopViewPager(context) }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView()
    }


    private fun initView() {
        viewPager.layoutParams =
            LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        viewPager.addOnPageChangeListener(this)
        this.addView(viewPager)
        initViewPagerScroll()
    }

    private fun initViewPagerScroll() {
        val mField = ViewPager::class.java.getDeclaredField("mScroller")
        mField.isAccessible = true
        val mScroller = AutoLoopScroller(context)
        mField.set(viewPager, mScroller)
    }


    fun setViewData(list: ArrayList<Int>) {
        val viewList = ArrayList<View>()
        size = list.size
        for (i in 0..size + 1) {
            val imageId = when (i) {
                0 -> {
                    list[size - 1]
                }
                size + 1 -> {
                    list[0]
                }
                else -> {
                    list[i - 1]
                }
            }
            val imageView = ImageView(context)
            imageView.setImageResource(imageId)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
            viewList.add(imageView)
        }
        if (viewPagerAdapter == null) {
            viewPagerAdapter = AutoLoopBannerAdapter(viewList)
            viewPager.adapter = viewPagerAdapter
        } else {
            viewPagerAdapter?.updateData(viewList)
        }
        viewPager.isFocusable = true
        viewPager.currentItem = 1
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        Log.i(TAG, "onPageSelected:   currentItem:$currentItem --- position:$position")
        currentItem = position
    }

    override fun onPageScrollStateChanged(state: Int) {
        Log.i(TAG, "onPageScrollStateChanged   currentItem:$currentItem   --- state:$state")
        when (state) {
            0 -> {
                if (currentItem == 0) {
                    viewPager.setCurrentItem(size, false)
                } else if (currentItem == size + 1) {
                    viewPager.setCurrentItem(1, false)
                }
            }
            1 -> {
                if (currentItem == size + 1) {
                    viewPager.setCurrentItem(1, false)
                } else if (currentItem == 0) {
                    viewPager.setCurrentItem(size, false)
                }
            }
            2 -> {
            }
        }
    }
}